package GUI;

import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import javax.swing.*;
import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;
import java.awt.Font;
import Signup.Account;
import Signup.AccountManagement;

import java.awt.Component;

public class SignUp extends javax.swing.JFrame {

	private javax.swing.JButton jButton1;
	private javax.swing.JButton jButton2;
	private javax.swing.JButton jButton3;
	private javax.swing.JLabel lb_id;
	private javax.swing.JLabel lb_name;
	private javax.swing.JLabel lb_pw;
	private javax.swing.JLabel lb_repw;
	private javax.swing.JLabel jLabel5;
	private javax.swing.JPanel jPanel1;
	private javax.swing.JPasswordField tfpw;
	private javax.swing.JPasswordField tfpw2;
	private javax.swing.JTextField tfid;
	private javax.swing.JTextField tfname;
	EventHandling eh = new EventHandling();

	public SignUp() {
		initComponents();
	}

	@SuppressWarnings("unchecked")
	private void initComponents() {

		final ImageIcon main1 = new ImageIcon("src/source/main/슬라이드11.png");

		jPanel1 = new javax.swing.JPanel() {
			public void paintComponent(Graphics g) {
				g.drawImage(main1.getImage(), 0, 0, null);
				setOpaque(false);
			}
		};

		tfid = new javax.swing.JTextField();
		tfname = new javax.swing.JTextField();
		lb_id = new javax.swing.JLabel();
		lb_name = new javax.swing.JLabel();
		lb_pw = new javax.swing.JLabel();
		lb_repw = new javax.swing.JLabel();
		jButton1 = new javax.swing.JButton();
		jLabel5 = new javax.swing.JLabel();
		tfpw = new javax.swing.JPasswordField();
		tfpw2 = new javax.swing.JPasswordField();
		jButton3 = new javax.swing.JButton();
		jButton2 = new javax.swing.JButton();

		setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

		this.setSize(975, 770);
		this.setLocationRelativeTo(null);

		lb_id.setFont(new Font("Consolas", Font.BOLD, 18)); // NOI18N
		lb_id.setForeground(new java.awt.Color(0, 153, 153));
		lb_id.setText("ID : ");

		lb_name.setFont(new Font("Consolas", Font.BOLD, 18)); // NOI18N
		lb_name.setForeground(new java.awt.Color(0, 153, 153));
		lb_name.setText("Name : ");

		lb_pw.setFont(new Font("Consolas", Font.BOLD, 18)); // NOI18N
		lb_pw.setForeground(new java.awt.Color(0, 153, 153));
		lb_pw.setText("Password : ");

		lb_repw.setFont(new Font("Consolas", Font.BOLD, 18)); // NOI18N
		lb_repw.setForeground(new java.awt.Color(0, 153, 153));
		lb_repw.setText("Re-enter PW : ");

		jButton1.setFont(new java.awt.Font("Segoe UI Emoji", 0, 14)); // NOI18N
		jButton1.setForeground(new java.awt.Color(0, 153, 153));
		jButton1.setText("Register");

		jLabel5.setFont(new Font("Cul De Sac", Font.PLAIN, 43)); // NOI18N
		jLabel5.setForeground(new java.awt.Color(0, 153, 153));
		jLabel5.setText("Ajou zoo Register");

		jButton3.setFont(new java.awt.Font("Segoe UI Emoji", 0, 14)); // NOI18N
		jButton3.setForeground(new java.awt.Color(0, 153, 153));
		jButton3.setText("Reset");
		jButton3.addActionListener(eh);

		jButton2.setBackground(new java.awt.Color(0, 153, 153));
		jButton2.setFont(new java.awt.Font("Segoe UI Emoji", 1, 14)); // NOI18N
		jButton2.setForeground(new java.awt.Color(102, 102, 102));

		jButton2.setText("Main!");

		jButton2.addActionListener(eh);

		jButton1.addActionListener(eh);

		javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(
				jPanel1);
		jPanel1Layout
				.setHorizontalGroup(jPanel1Layout
						.createParallelGroup(Alignment.TRAILING)
						.addGroup(
								jPanel1Layout
										.createSequentialGroup()
										.addGap(252)
										.addGroup(
												jPanel1Layout
														.createParallelGroup(
																Alignment.TRAILING)
														.addComponent(lb_repw)
														.addComponent(lb_id)
														.addComponent(lb_name)
														.addComponent(lb_pw))
										.addGap(18)
										.addGroup(
												jPanel1Layout
														.createParallelGroup(
																Alignment.LEADING)
														.addComponent(
																tfid,
																GroupLayout.DEFAULT_SIZE,
																238,
																Short.MAX_VALUE)
														.addComponent(tfpw,
																238, 238, 238)
														.addComponent(tfpw2,
																238, 238, 238)
														.addComponent(tfname,
																238, 238, 238))
										.addGap(312))
						.addGroup(
								jPanel1Layout
										.createSequentialGroup()
										.addContainerGap(304, Short.MAX_VALUE)
										.addComponent(jButton1,
												GroupLayout.PREFERRED_SIZE,
												166, GroupLayout.PREFERRED_SIZE)
										.addPreferredGap(
												ComponentPlacement.RELATED)
										.addComponent(jButton3,
												GroupLayout.PREFERRED_SIZE,
												158, GroupLayout.PREFERRED_SIZE)
										.addGap(316))
						.addGroup(
								jPanel1Layout.createSequentialGroup()
										.addGap(859).addComponent(jButton2)
										.addGap(0, 28, Short.MAX_VALUE))
						.addGroup(
								jPanel1Layout
										.createSequentialGroup()
										.addContainerGap(274, Short.MAX_VALUE)
										.addComponent(jLabel5,
												GroupLayout.PREFERRED_SIZE,
												478, GroupLayout.PREFERRED_SIZE)
										.addGap(208)));
		jPanel1Layout
				.setVerticalGroup(jPanel1Layout
						.createParallelGroup(Alignment.LEADING)
						.addGroup(
								jPanel1Layout
										.createSequentialGroup()
										.addGap(28)
										.addComponent(jButton2,
												GroupLayout.PREFERRED_SIZE, 45,
												GroupLayout.PREFERRED_SIZE)
										.addGap(28)
										.addComponent(jLabel5,
												GroupLayout.PREFERRED_SIZE, 83,
												GroupLayout.PREFERRED_SIZE)
										.addGap(26)
										.addGroup(
												jPanel1Layout
														.createParallelGroup(
																Alignment.BASELINE)
														.addComponent(
																tfid,
																GroupLayout.PREFERRED_SIZE,
																46,
																GroupLayout.PREFERRED_SIZE)
														.addComponent(lb_id))
										.addPreferredGap(
												ComponentPlacement.RELATED)
										.addGroup(
												jPanel1Layout
														.createParallelGroup(
																Alignment.BASELINE)
														.addComponent(
																tfname,
																GroupLayout.PREFERRED_SIZE,
																46,
																GroupLayout.PREFERRED_SIZE)
														.addComponent(lb_name))
										.addPreferredGap(
												ComponentPlacement.RELATED)
										.addGroup(
												jPanel1Layout
														.createParallelGroup(
																Alignment.BASELINE)
														.addComponent(
																tfpw,
																GroupLayout.PREFERRED_SIZE,
																47,
																GroupLayout.PREFERRED_SIZE)
														.addComponent(lb_pw))
										.addGap(7)
										.addGroup(
												jPanel1Layout
														.createParallelGroup(
																Alignment.BASELINE)
														.addComponent(
																tfpw2,
																GroupLayout.PREFERRED_SIZE,
																48,
																GroupLayout.PREFERRED_SIZE)
														.addComponent(lb_repw))
										.addGap(67)
										.addGroup(
												jPanel1Layout
														.createParallelGroup(
																Alignment.BASELINE)
														.addComponent(
																jButton1,
																GroupLayout.PREFERRED_SIZE,
																33,
																GroupLayout.PREFERRED_SIZE)
														.addComponent(
																jButton3,
																GroupLayout.PREFERRED_SIZE,
																33,
																GroupLayout.PREFERRED_SIZE))
										.addContainerGap(197, Short.MAX_VALUE)));
		jPanel1Layout.linkSize(SwingConstants.VERTICAL, new Component[] { tfid,
				tfname, tfpw, tfpw2 });
		jPanel1Layout.linkSize(SwingConstants.HORIZONTAL, new Component[] {
				jButton1, jButton3 });
		jPanel1.setLayout(jPanel1Layout);

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
				getContentPane());
		layout.setHorizontalGroup(layout.createParallelGroup(Alignment.LEADING)
				.addGroup(
						Alignment.TRAILING,
						layout.createSequentialGroup()
								.addContainerGap()
								.addComponent(jPanel1,
										GroupLayout.DEFAULT_SIZE,
										GroupLayout.DEFAULT_SIZE,
										Short.MAX_VALUE)));
		layout.setVerticalGroup(layout.createParallelGroup(Alignment.LEADING)
				.addGroup(
						Alignment.TRAILING,
						layout.createSequentialGroup()
								.addContainerGap()
								.addComponent(jPanel1,
										GroupLayout.DEFAULT_SIZE,
										GroupLayout.DEFAULT_SIZE,
										Short.MAX_VALUE)));
		getContentPane().setLayout(layout);
		pack();
	}

	public class EventHandling implements ActionListener {

		ArrayList<Account> accountlist = new ArrayList<Account>();

		@SuppressWarnings("deprecation")
		public void actionPerformed(ActionEvent e) {
			if (e.getSource().equals(jButton1)) {
				if (!tfid.getText().equals("") && !tfname.getText().equals("")
						&& !tfpw.getText().equals("")
						&& !tfpw2.getText().equals("")) {
					if (tfpw.getText().equals(tfpw2.getText())) {
						Account ac = new Account(tfid.getText(),
								tfname.getText(), tfpw.getText());
						accountlist.add(ac);
						try {
							FileOutputStream fs = new FileOutputStream(
									"accountdata.ser");
							ObjectOutputStream os = new ObjectOutputStream(fs);
							os.writeObject(accountlist);
							os.close();
						} catch (Exception ex) {
							ex.printStackTrace();
						}

						JOptionPane.showMessageDialog(null, "Complete SignUp");
						setVisible(false);
					} else
						JOptionPane
								.showMessageDialog(null, "re-enter Password");
				}
			} else if (e.getSource().equals(jButton2)) {
				setVisible(false);
			} else if (e.getSource().equals(jButton3)) {
				tfid.setText("");
				tfname.setText("");
				tfpw.setText(null);
				tfpw2.setText(null);
			}
		}
	}
}